# -*- coding: utf-8 -*- 
'''
Created on 2 sept. 2012

@author: Cybil
'''
from Server.Server import Server
from Client.Client import Client
from Data.Player import Player

class Controller(object):
    '''
    classdocs
    '''

    def __init__(self, inMainGUI):
        '''
        Constructor
        '''
        self.__mMainGUI = inMainGUI
        inMainGUI.SetController(self)
        self.__mServer  = None
        self.__mClient  = None
        self.__mPlayerList  = []
        self.__mPlayer = None
        
    def ConnectServeur(self, inIp, inPort, inPlayerName):
        self.__mPlayer = Player(inPlayerName)
        self.__mClient = Client(inIp, inPort, self)
        self.__mClient.start()
        
    def SetPlayerList(self, inNewPlayerList):
        for theElt in inNewPlayerList:
            if theElt.GetName() == self.GetPlayer().GetName():
                self.__mPlayer = theElt
                break
        self.__mPlayerList = inNewPlayerList
        self.__mMainGUI.UpdateListPlayer()
        
    def AddClickPlayer(self, inClickPlayer):
        self.__mMainGUI.AddClickPlayer(inClickPlayer)
        
    def HostServeur(self, inIp, inPort, inPlayerName):
        self.__mPlayer = Player(inPlayerName)
        self.__mServer = Server(inIp, inPort, self)
        self.__mClient = Client('127.0.0.1', inPort, self)
        self.__mServer.start()
        self.__mClient.start()
        
    def GetPlayerList(self):
        return self.__mPlayerList
        
    def GetClient(self):
        return self.__mClient
    
    def GetServer(self):
        return self.__mServer
           
    def GetPlayer(self):
        return self.__mPlayer
        
    def SendObject(self, inObj):
        if inObj is not None and self.__mClient is not None:
            self.__mClient.Send(inObj)
            
    def WriteTchatPlayer(self, inTchatPlayer):
        self.__mMainGUI.WriteTchatPlayer(inTchatPlayer)
        
    def SendClickPlayer(self, inClickPlayer):
        self.SendObject(inClickPlayer)  
            
    def SendMovePlayer(self, inMovePlayer):
        self.SendObject(inMovePlayer)
        
    def AddMovePlayer(self, inMovePlayer):
        self.__mMainGUI.AddMovePlayer(inMovePlayer)
  
    def GetStringScore(self):
        theStr = "Score : "
        for thePlayer in self.__mPlayerList:
            theStr += str(thePlayer) + ' || '
        return theStr
          
    def UpdatePlayer(self, inUpdatePlayer):
        for thePlayer in self.__mPlayerList:
            if thePlayer.GetName() == inUpdatePlayer.GetPlayer().GetName():
                thePlayer.SetScore(inUpdatePlayer.GetPlayer().GetScore())
                break
        if self.__mPlayer.GetName() == inUpdatePlayer.GetPlayer():
            self.__mPlayer.SetScore(inUpdatePlayer.GetPlayer().GetScore())
        self.__mMainGUI.UpdateScore()
        
        
        